package com.ihooyah;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
import org.htmlparser.visitors.TextExtractingVisitor;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );


        Person person = new Person();
        person.setName("licheng");
        person.setAge(18);

        Person person1 = (Person) person.clone();

        System.out.printf(person1.getName()+person1.getAge());

        //String url = "http://www.hglws.com/foli/module_foli.js";
        //extractKeyWordText(url, "");
    }


    //清单1：获取网页上的所有节点
    public static void extractKeyWordText(String url, String keyword) {
        try {
            //生成一个解析器对象，用网页的 url 作为参数
            Parser parser = new Parser(url);
            //设置网页的编码,这里只是请求了一个 gb2312 编码网页
            parser.setEncoding("GB2312");
            //迭代所有节点, null 表示不使用 NodeFilter
            NodeList list = parser.parse(null);
            //从初始的节点列表跌倒所有的节点
            processNodeList(list, keyword);
        } catch (ParserException e) {
            e.printStackTrace();
        }
    }


    private static void processNodeList(NodeList list, String keyword) {
        //迭代开始
        SimpleNodeIterator iterator = list.elements();
        while (iterator.hasMoreNodes()) {
            Node node = iterator.nextNode();
            //得到该节点的子节点列表
            NodeList childList = node.getChildren();
            //孩子节点为空，说明是值节点
            if (null == childList)
            {
                //得到值节点的值
                String result = node.toPlainTextString();
                //若包含关键字，则简单打印出来文本
                if (result.indexOf(keyword) != -1)
                    System.out.println(result);
            } //end if
            //孩子节点不为空，继续迭代该孩子节点
            else
            {
                processNodeList(childList, keyword);
            }//end else
        }//end wile
    }


    //清单2：// 获取一个网页上所有的链接和图片链接
    public static void extracLinks(String url) {
        try {
            Parser parser = new Parser(url);
            parser.setEncoding("gb2312");
            //过滤 <frame> 标签的 filter，用来提取 frame 标签里的 src 属性所、表示的链接
            NodeFilter frameFilter = new NodeFilter() {
                public boolean accept(Node node) {
                    if (node.getText().startsWith("frame src=")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            };
            //OrFilter 来设置过滤 <a> 标签，<img> 标签和 <frame> 标签，三个标签是 or 的关系
            OrFilter orFilter = new OrFilter(new NodeClassFilter(LinkTag.class), new
                    NodeClassFilter(ImageTag.class));
            OrFilter linkFilter = new OrFilter(orFilter, frameFilter);
            //得到所有经过过滤的标签
            NodeList list = parser.extractAllNodesThatMatch(linkFilter);
            for (int i = 0; i < list.size(); i++) {
                Node tag = list.elementAt(i);
                if (tag instanceof LinkTag)//<a> 标签
                {
                    LinkTag link = (LinkTag) tag;
                    String linkUrl = link.getLink();//url
                    String text = link.getLinkText();//链接文字
                    System.out.println(linkUrl + "**********" + text);
                }
                else if (tag instanceof ImageTag)//<img> 标签
                {
                    ImageTag image = (ImageTag) list.elementAt(i);
                    System.out.print(image.getImageURL() + "********");//图片地址
                    System.out.println(image.getText());//图片文字
                }
                else//<frame> 标签
                {
//提取 frame 里 src 属性的链接如 <frame src="test.html"/>
                    String frame = tag.getText();
                    int start = frame.indexOf("src=");
                    frame = frame.substring(start);
                    int end = frame.indexOf(" ");
                    if (end == -1)
                        end = frame.indexOf(">");
                    frame = frame.substring(5, end - 1);
                    System.out.println(frame);
                }
            }
        } catch (ParserException e) {
            e.printStackTrace();
        }
    }

    private static String ENCODE = "gb2312";
    private static void message( String szMsg ) {
        try{
            System.out.println(new String(szMsg.getBytes(ENCODE), System.getProperty("file.encoding")));
        } catch(Exception e )
        {

        }
    }
    public static String openFile( String szFileName ) {
        try {
            BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream( new File(szFileName)), ENCODE) );
            String szContent="";
            String szTemp;

            while ( (szTemp = bis.readLine()) != null) {
                szContent+=szTemp+"\n";
            }
            bis.close();
            return szContent;
        }
        catch( Exception e ) {
            return "";
        }
    }


    private static void httpTest() {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        try{
            //创建httpget
            //HttpGet httpget = new HttpGet("http://rs.xidian.edu.cn/forum.php");
            HttpGet httpget = new HttpGet("http://www.hglws.com/foli/include_foli.htm?TZ=-1200+%B0%B2%C4%E1%CD%FE%CD%C1%BF%CB%A1%A2%B9%CF%BC%D7%C0%BC&SY=2017&SM=12");
            System.out.println("executing request " + httpget.getURI());
            //List<NameValuePair> params = new ArrayList<NameValuePair>();
            //params.add(new BasicNameValuePair("wd","wq"));
            //String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
            //httpget.setURI(new URI(httpget.getURI().toString() +"?" + str));


            //执行get请求
            CloseableHttpResponse response = httpclient.execute(httpget);
            try{
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //响应状态
                System.out.println(response.getStatusLine());
                if(entity != null){
                    //内容长度
                    System.out.println("Response content length: " + entity.getContentLength());
                    //响应内容
                    System.out.println("Response content: " + EntityUtils.toString(entity, Charset.forName("gb2312")));

                    System.out.printf("编码为：%s",response.getEntity().getContentEncoding());

                }
            }finally{
                response.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                httpclient.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }


    ////获取子链接，url为网页url，filter是链接过滤器，返回该页面子链接的HashSet
    //public static Set<String> extracLinks(String url, LinkFilter filter) {
    //
    //    Set<String> links = new HashSet<String>();
    //    try {
    //        Parser parser = new Parser(url);
    //        parser.setEncoding("utf-8");
    //        // 过滤 <frame >标签的 filter，用来提取 frame 标签里的 src 属性所表示的链接
    //        NodeFilter frameFilter = new NodeFilter() {
    //            public boolean accept(Node node) {
    //                if (node.getText().startsWith("frame src=")) {
    //                    return true;
    //                } else {
    //                    return false;
    //                }
    //            }
    //        };
    //        // OrFilter 接受<a>标签或<frame>标签，注意NodeClassFilter()可用来过滤一类标签，linkTag对应<标签>
    //        OrFilter linkFilter = new OrFilter(new NodeClassFilter(
    //                LinkTag.class), frameFilter);
    //        // 得到所有经过过滤的标签，结果为NodeList
    //        NodeList list = parser.extractAllNodesThatMatch(linkFilter);
    //        for (int i = 0; i < list.size(); i++) {
    //            Node tag = list.elementAt(i);
    //            if (tag instanceof LinkTag)// <a> 标签
    //            {
    //                LinkTag link = (LinkTag) tag;
    //                String linkUrl = link.getLink();// 调用getLink()方法得到<a>标签中的链接
    //                if (filter.accept(linkUrl))//将符合filter过滤条件的链接加入链接表
    //                    links.add(linkUrl);
    //            } else{// <frame> 标签
    //                // 提取 frame 里 src 属性的链接如 <frame src="test.html"/>
    //                String frame = tag.getText();
    //                int start = frame.indexOf("src=");
    //                frame = frame.substring(start);
    //                int end = frame.indexOf(" ");
    //                if (end == -1)
    //                    end = frame.indexOf(">");
    //                String frameUrl = frame.substring(5, end - 1);
    //                if (filter.accept(frameUrl))
    //                    links.add(frameUrl);
    //            }
    //        }
    //    } catch (ParserException e) {//捕捉parser的异常
    //        e.printStackTrace();
    //    }
    //    return links;
    //}
}
